package com.example.wecompose

import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.wecompose.data.Chat
import com.example.wecompose.data.Msg
import com.example.wecompose.data.User
import com.example.wecompose.ui.theme.WeComposeTheme
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn

class WeViewModel : ViewModel() {
    //想要被监听就需要用到mutableState,他的改变才会触发界面的刷新

    var theme by mutableStateOf(WeComposeTheme.Theme.Light)

    val isLightTheme by derivedStateOf { theme == WeComposeTheme.Theme.Light }
    val isLightThemeFlow = snapshotFlow { isLightTheme }
        .stateIn(viewModelScope, SharingStarted.Lazily,true)

    var chats by mutableStateOf(
        listOf(
            Chat(
                friend = User("gaolaoshi","法外狂徒",R.drawable.icon2),
                mutableStateListOf(
                    Msg(User("gaolaoshi","法外狂徒",R.drawable.icon2),"苏格拉底说","14:20"),
                    Msg(User.Me,"哦","14:20"),
                    Msg(User("gaolaoshi","法外狂徒",R.drawable.icon2),"我只知道一件事情","14:20"),
                    Msg(User.Me,"哦","14:20"),
                    Msg(User("gaolaoshi","法外狂徒",R.drawable.icon2),"那就是我一无所知","14:20"),
                    Msg(User.Me,"哦","14:20"),
                    Msg(User("gaolaoshi","法外狂徒",R.drawable.icon2),"夜郎自大，是无知者和好辩者的天性","14:20"),
                    Msg(User.Me,"哇哈哈哈哈哈哈哈哈哈哈","14:20"),
                    )

            ),Chat(
                friend = User("zhangsan","张三",R.drawable.icon3),
                mutableStateListOf(
                    Msg(User("zhangsan","张三",R.drawable.icon3),"若非命运的加持","14:20"),
                    Msg(User.Me,"哦","14:20"),
                    Msg(User("zhangsan","张三",R.drawable.icon3),"少年时的理想不值得一提","14:20").apply { read = false },

                )

            )
        )
    )

    var contacts by mutableStateOf(
        listOf(
            User("gaolaoshi","法外狂徒",R.drawable.icon2),
            User("zhangsan","张三",R.drawable.icon3)
        )
    )

    val userInfo: User = User.Me


    fun switchTheme(){
        theme = when (theme) {
            WeComposeTheme.Theme.Light -> WeComposeTheme.Theme.Dark
            WeComposeTheme.Theme.Dark -> WeComposeTheme.Theme.NewYear
            WeComposeTheme.Theme.NewYear -> WeComposeTheme.Theme.Light
        }

    }

    fun boom(chat: Chat) {
        chat.msgs.add(Msg(User.Me,"\uD83D\uDCA3","16:00").apply { read =true })
    }


}